package com.zhuojy.equity.mall.admin.service.car;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.TCarUse;
import com.zhuojy.equity.mall.admin.common.entity.po.car.TCarUsePO;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.dal.mapper.car.TCarUseMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xrz
 * @version 1.0
 * @description:TODO
 * @date 2024/3/28 15:27
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TCarUseService extends ServiceImpl<TCarUseMapper, TCarUsePO> {


    public ResultEntity findTCarUseCenterById(Integer id) {
        TCarUsePO tCarUseCenterById = this.baseMapper.findTCarUseCenterById(id);
        return ResultEntity.OK(tCarUseCenterById);
    }

    public ResultEntity getCarUser(TCarUsePO tCarUsePO, Integer pageSize, Integer pageNum) {
        Page<TCarUse> page = new Page<>(pageNum, pageSize);
        IPage<TCarUsePO> tCarusePage = this.baseMapper.findCarUsePagination(tCarUsePO, page);
        List<TCarUsePO> records = tCarusePage.getRecords();
        for (TCarUsePO e : records) {
            long diff = e.getCarEndTime().getTime() - e.getCarStartTime().getTime();//这样得到的差值是毫秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            if (days!=0 & hours!=0){
                e.setDrivingTime(days+"天"+hours+"小时");
            }else if (days!=0 & hours==0){
                e.setDrivingTime(days+"天");
            }else if (days==0 & hours!=0){
                e.setDrivingTime(hours+"小时");
            }
        }
        return ResultEntity.OK(tCarusePage);
    }

    public ResultEntity addCarUse(TCarUsePO tCarUsePO) {
        tCarUsePO.setCreateTime(new Date());
        int insert = this.baseMapper.insert(tCarUsePO);
        return ResultEntity.OK("添加成功");
    }

    public ResultEntity updateCarUserState(TCarUsePO tCarUsePO) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",tCarUsePO.getId());
        this.baseMapper.update(tCarUsePO,wrapper);
        return ResultEntity.OK("操作成功");
    }
}
